package com.wswy.wzcx.ui.main.home

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.PixelFormat
import android.os.Build
import android.view.*
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import com.wswy.wzcx.AppContext
import com.wswy.wzcx.R
import com.wswy.wzcx.model.license.UserCarInfo
import com.wswy.wzcx.module.base.gone
import com.wswy.wzcx.module.base.visible
import com.wswy.wzcx.statistics.StatTools
import com.wswy.wzcx.statistics.StatisticsId
import com.wswy.wzcx.ui.car.result.QueryResultActivity
import timber.log.Timber


object Tips {

    @JvmStatic
    fun show(context: Context) {

        val wm: WindowManager = context.getSystemService(Context.WINDOW_SERVICE) as? WindowManager
                ?: return

        LayoutInflater.from(context).inflate(R.layout.home_layout_tips, null)?.let {

            val params = WindowManager.LayoutParams(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.WRAP_CONTENT).apply {

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
                } else {
                    type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR
                }

                flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or
                        WindowManager.LayoutParams.FLAG_FULLSCREEN or
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL


                format = PixelFormat.TRANSLUCENT

                gravity = Gravity.TOP or Gravity.LEFT
                x = 0
                y = 0
            }



            wm.addView(it, params)

            it.postDelayed({
                wm.removeView(it)
            }, 3000)

        }

    }


    @JvmStatic
    fun showView(rootView:FrameLayout,context: Context,userCarInfo: UserCarInfo){


        LayoutInflater.from(context).inflate(R.layout.home_layout_tips, rootView,false)?.let {


            val hasFine = userCarInfo.trafficCount > 0

            it.findViewById<TextView>(R.id.tv_title).text = if (hasFine)
                context.getString(R.string.home_tips_fine, userCarInfo.carNo, userCarInfo.trafficCount)
            else context.getString(R.string.home_tips_no_fine, userCarInfo.carNo)


            it.findViewById<TextView>(R.id.tv_more).text = if (hasFine) "点击查看详情" else "请继续保持~"

            it.findViewById<ImageView>(R.id.img_icon).setImageResource(if (hasFine) R.drawable.notic_order_fadandaijiao else R.drawable.notice_good)

            if (hasFine){
                it.findViewById<View>(R.id.img_right).visible()
            }else{
                it.findViewById<View>(R.id.img_right).gone()
            }


            //it.visibility = View.INVISIBLE
            val desiredWidth = View.MeasureSpec.makeMeasureSpec(rootView.width,
                    View.MeasureSpec.AT_MOST)

            it.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED)

            val h = it.measuredHeight.toFloat()

            if (h < 10) {
                return
            }

            if (userCarInfo.trafficCount > 0) {

                it.setOnClickListener {

                    StatTools.sendClick(AppContext.getContext(),StatisticsId.home_fine_tips)

                    QueryResultActivity.start(it.context, userCarInfo, -1)
                }
            }

            rootView.addView(it, ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))


//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//
//                val finalRadius = Math.hypot(it.measuredWidth.toDouble(), it.measuredHeight.toDouble()).toFloat()
//
//                Timber.e("  finalRadius  $finalRadius   ${it.measuredWidth}   ${it.measuredHeight}")
//
//                ViewAnimationUtils.createCircularReveal(it, 0, 0, 0f, finalRadius).run {
//
//                    duration = 400
//                    interpolator = AccelerateDecelerateInterpolator()
//
//                    addListener(getAnim(rootView, it))
//
//                    start()
//                }
//            } else {
//
//                ObjectAnimator.ofFloat(it, View.TRANSLATION_Y, -h, 0f)
//                        .apply {
//                            addListener(getAnim(rootView, it))
//                        }
//                        .start()
//
//            }


            ObjectAnimator.ofFloat(it, View.TRANSLATION_Y, -h, 0f)
                    .apply {
                        addListener(getAnim(rootView, it))
                    }
                    .start()
        }

    }



    private fun getAnim(rootView: FrameLayout,itemView:View):AnimatorListenerAdapter{
        return object :AnimatorListenerAdapter(){
            override fun onAnimationEnd(animation: Animator?) {
                super.onAnimationEnd(animation)
                rootView.postDelayed({
                    hide(rootView, itemView)
                }, 3000)
            }
        }
    }


    private fun hide(rootView: FrameLayout,itemView:View){
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            val finalRadius = Math.hypot(itemView.measuredWidth.toDouble(), itemView.measuredHeight.toDouble()).toFloat()
//            ViewAnimationUtils.createCircularReveal(itemView,0,0,finalRadius,0f).run {
//
//                duration = 400
//                interpolator = AccelerateDecelerateInterpolator()
//
//                addListener(object : AnimatorListenerAdapter() {
//
//                    override fun onAnimationEnd(animation: Animator?) {
//                        super.onAnimationEnd(animation)
//                        rootView.removeView(itemView)
//                    }
//                })
//
//                start()
//            }
//        }else {
//
//            ObjectAnimator.ofFloat(itemView, View.ALPHA, 1f, 0f).run {
//
//                addListener(object : AnimatorListenerAdapter() {
//
//                    override fun onAnimationEnd(animation: Animator?) {
//                        super.onAnimationEnd(animation)
//                        rootView.removeView(itemView)
//                    }
//                })
//
//                start()
//            }
//        }


        ObjectAnimator.ofFloat(itemView, View.TRANSLATION_Y,  0f,-itemView.measuredHeight.toFloat())
                .run {
                    addListener(object : AnimatorListenerAdapter() {

                        override fun onAnimationEnd(animation: Animator?) {
                            super.onAnimationEnd(animation)
                            rootView.removeView(itemView)
                        }
                    })
                    start()
                }


    }

}